#include "snowball.h"

#include "explosion.h"
#include "terrain.h"
#include "player.h"

class CSnowman;

CSnowBall::CSnowBall()
{
	velocity = CVector(0.0, 0.0, 250.0);
	acceleration = CVector(0.0, 0.0, 0.0);
	gravity = 0.0f;
	distanceTravel = 0.0;
	size = 1.0f;
	isExplosion = false;
	usedUp = false;

	explosion = NULL;
	explosionTex = new CTexture;

	sphere = gluNewQuadric();

	Load();
}

CSnowBall::~CSnowBall()
{
	if (explosion != NULL)
	{
		explosion->KillSystem();
		delete [] explosion;
		explosion = NULL;
	}

	if (explosionTex != NULL)
	{
		delete explosionTex;
		explosionTex = NULL;
	}
}

void CSnowBall::SetupExplosionTexture()
{
	glGenTextures(1, &explosionTex->texID);
	glBindTexture(GL_TEXTURE_2D, explosionTex->texID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, explosionTex->width, explosionTex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, explosionTex->width, explosionTex->height, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
}

void CSnowBall::OnAnimate(scalar_t deltaTime)
{
	//This snowball (previously rocket) distance calculation code is much more accurate.
	//distanceTravel no longer needs to be an absurdly large number. -SR
	float speed = (velocity.z - 1) * deltaTime;

	CVector delta = CVector((float)cos(DEG2RAD(direction)),
							(float)sin(DEG2RAD(pitch)),
							(float)sin(DEG2RAD(direction))) * speed;
	delta.y -= gravity;
	position = position + delta;

	gravity += deltaTime/1.15f;

	distanceTravel += delta.Length();

	if (isExplosion)
		explosion->Update(deltaTime);

	if (!isExplosion)
	{
		if (distanceTravel >= 3000.0f)
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(10, position, 0.1f, explosionTex->texID);
		}
	}
}

void CSnowBall::OnCollision(CObject *collisionObject)
{
	if (!isExplosion)
	{
		if (typeid(*collisionObject) == typeid(CTerrain))
		{
			if (((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size >= position.y)
			{
				isExplosion = true;
				velocity = CVector(0.0, 0.0, 0.0);
				explosion = new CExplosion(1, position, 1.0, explosionTex->texID);

				// Ian Bell - 12/1/2012
				// I'd like to turn this into a function
				CVector diff = player->position - position;
				// Get distance to player
				float distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);

				if((distFromPlayer < 75))
					audioSys->PlaySoundDX(audioSys->iSnowBallSplat);
				else if(distFromPlayer < 200)
					audioSys->PlaySoundDX(audioSys->iSnowBallSplat_low);
			}
		
			// do explosion
		}
		if (typeid(*collisionObject) == typeid(CSnowman))
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(1, position, 1.0, explosionTex->texID);

			// Ian Bell - 12/1/2012
			// I'd like to turn this into a function
			CVector diff = player->position - position;
			// Get distance to player
			float distFromPlayer = sqrt(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);

			if((distFromPlayer < 75))
				audioSys->PlaySoundDX(audioSys->iSnowBallSplat);
			else if(distFromPlayer < 200)
				audioSys->PlaySoundDX(audioSys->iSnowBallSplat_low);
		}
	}
}

void CSnowBall::OnDraw(CCamera *camera)
{
	// if the rocket has not yet exploded, draw the rocket model
	if (!isExplosion)
	{
		glColor3f(0.975f, 0.975f, 0.975f);
		glTranslatef(position.x, position.y, position.z);
		gluSphere(sphere, size, 16, 16);
	}
	// draw explosion
	else
	{
		glDisable(GL_FOG);
		explosion->Render();
		glEnable(GL_FOG);
	}
}


void CSnowBall::Load()
{
	CMD2Model::Load("..\\..\\res\\models\\rocketair.md2", "..\\..\\res\\models\\rocket.pcx");
	explosionTex->LoadTexture("..\\..\\res\\explosion.bmp");
	SetupExplosionTexture();
}

void CSnowBall::Unload()
{
}

void CSnowBall::OnPrepare()
{
	// perform collision detection from this entity with all other objects in world
	if (!isExplosion)
		ProcessCollisions(FindRoot());

	if (isExplosion)
	{
		if (explosion->IsDead())
		{
			explosion->KillSystem();
			delete explosion;
			explosion = NULL;
			isExplosion = false;

			isDead = true;
		}
	}
}
